﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;

namespace LibFriesland_Campina_CRM
{
    public class DalBase
    {
        private MySqlConnection connection;

        public DalBase()
        { 
            
        }

        private void createConnection()
        {
            if (connection == null)
                connection = new MySqlConnection(Thuvien.getConnString());
            connection.Open();
        }

        public DataTable getData(string pCommandText)
        {
            DataTable result = new DataTable();
            createConnection();
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(pCommandText, connection);

            //dataAdapter.FillSchema(result, SchemaType.Mapped);
            dataAdapter.Fill(result);
            
            connection.Close();
            return result;
        }
        
        public int executeNonqueryCommand(string pCommandText)
        {
            try
            {
                createConnection();
                MySqlCommand command = new MySqlCommand(pCommandText, connection);
                int result = command.ExecuteNonQuery();
                connection.Close();
                return result;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }

        public object executeScalarCommand(string pCommandText)
        {
            try
            {
                createConnection();
                MySqlCommand command = new MySqlCommand(pCommandText, connection);
                object result = command.ExecuteScalar();
                connection.Close();
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
